#include <bits/stdc++.h>
using namespace std;
int tac,n,L=1000005;
int a[200010];
long long d[200010],tran[1000010];
long long pre[200010];
inline void Main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++) scanf("%d",&a[i]);
    a[n]=0; n++;
    pre[0]=0; for (int i=0;i<n;i++) pre[i+1]=pre[i]+((i>0 && a[i-1]==a[i]) ? a[i] : 0);
    fill(tran,tran+L,(long long)-1e18); long long tr=-1e18;
    fill(d,d+n,(long long)-1e18);
    d[0]=0; tr=max(tr,d[0]-pre[1]);
    for (int i=1;i<n;i++)
    {
        d[i]=max(d[i],tr+pre[i]);
        d[i]=max(d[i],tran[a[i]]+pre[i]+a[i]);
        tr=max(tr,d[i]-pre[i+1]);
        tran[a[i-1]]=max(tran[a[i-1]],d[i]-pre[i+1]);
    }
    printf("%lld\n",d[n-1]);
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&tac);
    while (tac--) Main();
    return 0;
}